package cn.edu.ujn.lab3.controller;

import cn.edu.ujn.lab3.entity.Customer;
import cn.edu.ujn.lab3.entity.Page;
import cn.edu.ujn.lab3.entity.user;
import cn.edu.ujn.lab3.service.ICustomerService;
import cn.edu.ujn.lab3.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

/**
 * @author heyao zhou
 * @create 2022-11-08 19:27
 */
@Controller
public class CustomerController {
        @Autowired
        private ICustomerService customerService;

        @Autowired
        private IUserService userService;
        @RequestMapping("/customer")
        public String ToIndex() {
            return "customer";
        }

        @PostMapping("/CustomerPage")
        @ResponseBody
        public List<Customer> findPage(@RequestBody Page page){
            return customerService.selectCustomerByPage(page);
        }

        @PostMapping("/selectAllCustomer")
        @ResponseBody
        public List<Customer> selectAll(){
            return customerService.findAll();
        }

        @PostMapping("/selectByCustomerMobile")
        @ResponseBody
        public List<Customer> selectByMobile(@RequestBody HashMap<String, String> map){
            String mobile = map.get("mobile");
            return customerService.selectByMobile(mobile);
        }

        @PostMapping("/selectByCustomerName")
        @ResponseBody
        public List<Customer> selectByName(@RequestBody HashMap<String, String> map){
            String name = map.get("name");
            return customerService.selectCustomerByName(name);

        }
        @PostMapping("/addCustomer")
        @ResponseBody
        public Boolean addCustomer(@RequestBody Customer customer){
            Boolean flag = customerService.addCustomer(customer);
            return flag;
        }

        @PostMapping("/updateCustomer")
        @ResponseBody
        public Boolean updateCustomer(@RequestBody Customer customer){
            Boolean flag = customerService.updateCustomer(customer);
            return flag;
        }

        @PostMapping("/deleteCustomer")
        @ResponseBody
        public Boolean deleteCustomer(@RequestBody HashMap<String, String> map){
            String id = map.get("id");
            Integer idd = Integer.parseInt(id);
            Boolean flag = customerService.deleteCustomer(idd);
            return flag;
        }

}
